const jQuery = require('jquery');
const path = require('path');
const epointajax = require(path.join(__dirname, '../../../js/epointajax'));
const common = require(path.join(__dirname, '../../../js/common'));
const {
  allconfig
} = require(path.join(__dirname, '../../../js/config'));
const {
  ipcRenderer
} = require('electron');


var pagevue, appinfo = {};
jQuery(function ($) {

  initPage(appinfo);

  function initPage(appinfo) {
    //渲染列表
    pagevue = new Vue({
      el: '#app',
      data: function () {
        var iconcheck = function (rule, value, callback) {
          if (pagevue.fileList.length == 0) {
            callback(new Error('图标不能为空'));
          } else {
            callback();
          }
        }
        return {
          form: {
            iconurl: appinfo.iconurl,
            appname: appinfo.appname,
            appurl: appinfo.appurl,
            operateusername: appinfo.operateusername,
            remark: appinfo.remark,
            public: appinfo.public,
            enablenode: appinfo.enablenode
          },
          fileList: [],
          iconchange: false,
          rules: {
            iconurl: [{
              validator: iconcheck,
              trigger: 'blur'
            }],
            appname: [{
              required: true,
              message: '请输入应用名称',
              trigger: 'blur'
            }],
            appurl: [{
              required: true,
              message: '地址必选',
              trigger: 'blur'
            }],
            remark: [{
              required: true,
              message: '备注必填',
              trigger: 'blur'
            }]
          }

        }
      },
      methods: {
        closepage() {
          ipcRenderer.send('pageMyappinfo');
          window.close();
        },
        appinfoadd(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              epointajax.post(allconfig.serverurl + '/rest/toolsrest/appinfo_add', {
                appguid: this.form.appguid,
                appname: this.form.appname,
                appurl: this.form.appurl,
                remark: this.form.remark,
                public: this.form.public,
                enablenode: this.form.enablenode
              }).then(retinfo => {
                let fileobj = this.fileList[0].raw;
                return epointajax.postfile(allconfig.serverurl + '/rest/toolsrest/appinfo_iconupload', {
                  appguid: retinfo.custom.appguid
                }, fileobj);

              }).then((retinfo) => {
                if (retinfo.status) {
                  this.$message({
                    message: retinfo.text,
                    type: 'success',
                    duration: 1500,
                    onClose: function () {
                      this.pagevue.closepage();
                    }
                  });
                }
              }).catch(msg => {
                console.log(msg);
              });
            } else {
              console.log('error submit!!');
              return false;
            }
          });


        },
        handleRemove(file) {
          this.fileList = this.fileList.filter(fileobj => fileobj.uid !== file.uid);
        },
        handleChange(file, fileList) {
          this.iconchange = true;
          file.name = '';
          this.fileList = fileList.slice(-3);
        }
      }

    });
  }


});
